package com.example.onlinetaxi.ui.activity.DriverToStartPage;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.R;
import com.example.onlinetaxi.Thread.IsDriverAgreeCancelThread;
import com.example.onlinetaxi.Thread.PassengerGetOrderStateThread;
import com.example.onlinetaxi.callback.DriverCallBack.ICancelCallBack;
import com.example.onlinetaxi.ui.activity.StartToEndPage.StartToEndActivity;
import com.example.onlinetaxi.util.OKHttpUtil.HttpUrl;
import com.example.onlinetaxi.util.OKHttpUtil.OKHttpUtil;
import com.example.onlinetaxi.view.Dialog.EditTextDialog;

import java.util.HashMap;
import java.util.Map;

// 等待司机前往
public class PassengerWaitingFragment extends Fragment {

    private View rootView;
    // 按钮
    private Button cancelBtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_passenger_waiting, container, false);

        // 取消按钮
        cancelBtn = rootView.findViewById(R.id.passenger_waiting_cancel_order);


        initEvent();

        return rootView;
    }

    private void initEvent() {
        // 设置"撤销订单"按钮监听事件
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 弹出确认框
                showDialog();
            }
        });

        // 乘客上车
        // 启动线程接收当前订单的状态
        // 乘客上车后自动进行跳转
        PassengerGetOrderStateThread.finished = false;
        PassengerGetOrderStateThread.thread = new Thread(new PassengerGetOrderStateThread.GetReceivedOrderState(getActivity(), StartToEndActivity.class));
        PassengerGetOrderStateThread.thread.start();
    }

    private void showDialog() {
        View.OnClickListener onCancelClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 取消，不操作
            }
        };

        View.OnClickListener onConfirmClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击确认
                // 向后端发送取消清单请求
                sendCancelOrderRequest();

                Toast.makeText(getActivity(), "撤销申请已发送!请等待司机响应!", Toast.LENGTH_SHORT).show();

                // 关闭获取状态线程
                PassengerGetOrderStateThread.finished = true;
                // 清除线程
                PassengerGetOrderStateThread.thread = null;

                // 开启线程监听司机是否同意取消
                IsDriverAgreeCancelThread.iCancelCallBack = new ICancelCallBack() {
                    @Override
                    public void cancelCallBack(String state) {
                        // 对state进行判断
                        if (state.equals("2")) {
                            // 司机同意
                            // 清除当前页面
                            getActivity().finish();

                            // 删除保存本地的订单数据
                            LocalStorage.chosenOrder = null;
                            LocalStorage.currentOrderId = null;
                            LocalStorage.currentOrder = null;

                            Looper.prepare();
                            Toast.makeText(getActivity(), "司机同意了您的撤销请求！", Toast.LENGTH_SHORT).show();
                            Looper.loop();
                        } else if (state.equals("3")) {
                            // 司机拒绝
                            // 重启获取司机是否接到乘客线程
                            PassengerGetOrderStateThread.finished = false;
                            PassengerGetOrderStateThread.thread = new Thread(new PassengerGetOrderStateThread.GetReceivedOrderState(getActivity(), StartToEndActivity.class));
                            PassengerGetOrderStateThread.thread.start();

                            Looper.prepare();
                            Toast.makeText(getActivity(), "司机拒绝了您的撤销请求！", Toast.LENGTH_SHORT).show();
                            Looper.loop();
                        }
                    }
                };
                IsDriverAgreeCancelThread.finished = false;
                IsDriverAgreeCancelThread.thread = new Thread(new IsDriverAgreeCancelThread.GetOrderState());
                IsDriverAgreeCancelThread.thread.start();
            }
        };

        // 显示提示框
        EditTextDialog.showInfoDialog(getActivity(), "撤销原因", "取消撤销", onCancelClickListener, "确认撤销", onConfirmClickListener);
    }

    private void sendCancelOrderRequest() {
        Map<String, String> map = new HashMap<>();

        map.put("orderId", LocalStorage.currentOrder.getReceivedOrderId().toString());
        map.put("cancelReason", EditTextDialog.Builder.cancelReason.getText().toString());

        OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "receivedOrder", "cancelOrderRequest");
    }
}